// 处理业务逻辑的中间件，读取某个json文件
const path = require('path')
const fileUtils = require('../utils/file_utils')
module.exports = async (ctx, next) => {
    // 获取url
    const url = ctx.request.url
    let filePath = url.replace('/api','')
    filePath = '../data' + filePath + '.json'
    filePath = path.join(__dirname, filePath)
    // console.log(filePath)
    try {
        const ret = await fileUtils.getFileJsonData(filePath)
        ctx.response.body = ret

    } catch (e) {
        const errMsg = {
            message: '文件资源读取出错',
            status: 404
        }
        ctx.response.body = JSON.stringify(errMsg)
    }
    await next()
}